﻿using System;
using System.Collections;
using System.Globalization;
using System.Windows.Data;

namespace Dsl.Ped.Common.Converters
{
    /// <summary>
    /// Converts collection into <see cref="Boolean"/>.
    /// </summary>
    public class CollectionToBoolConverter : IValueConverter
    {
        #region Implementation of IValueConverter

        /// <summary>
        /// Convert empty and null collection into true, otherwise false
        /// </summary>
        /// <returns>True if the collection is not empty and not null</returns>
        /// <exception cref="ArgumentException">If the value is not an <see cref="ICollection"/> type</exception>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return true;

            //empty collection return true
            if (value is ICollection) return (value as ICollection).Count == 0 ? true : false;

            throw new ArgumentException("Passed argument does not implement ICollection interface!");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
